const { onMsg, request } = require(`${process.root}/util`);
const CONFIG = require(`${process.root}/config`);
const baseUrl = CONFIG.bot.server;
const reg = /(?<content>签到)/;

module.exports = {
  name: 'sign-in',
  main: ({ data }) => {
    onMsg(data, async (msg, sender) => {
      const { user_id: id, nickname } = sender;
      const res = reg.exec(msg);
      if (!res?.groups?.content) return;
      try {
        await request({ url: baseUrl + '/qq/sign-in', data: { id }, method: 'PATCH' });
      } catch {
        await request({ url: baseUrl + '/qq/' + id, method: 'POST' })
      }
      try {
        const { data } = await request({ url: baseUrl + '/qq/' + id, method: 'GET' })
        const { sign_in_count } = data[0];
        return {
          message: `签到成功
  ${nickname}
  已签到${sign_in_count}天`
        }
      } catch { return { message: '签到失败' } }
    }, { checkAdmin: false, checkAt: true, checkName: true })
  }
}
